package com.ruoyi.bbs.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.bbs.domain.BbsPost;
import com.ruoyi.bbs.service.IBbsPostService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 帖子Controller
 * 
 * @author saruri
 * @date 2024-10-02
 */
@RestController
@RequestMapping("/post")
public class BbsPostController extends BaseController
{
    @Autowired
    private IBbsPostService bbsPostService;

    /**
     * 查询帖子列表
     */
    @RequiresPermissions("bbs:post:list")
    @GetMapping("/list")
    public TableDataInfo list(BbsPost bbsPost)
    {
        startPage();
        List<BbsPost> list = bbsPostService.selectBbsPostList(bbsPost);
        return getDataTable(list);
    }

    /**
     * 导出帖子列表
     */
    @RequiresPermissions("bbs:post:export")
    @Log(title = "帖子", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BbsPost bbsPost)
    {
        List<BbsPost> list = bbsPostService.selectBbsPostList(bbsPost);
        ExcelUtil<BbsPost> util = new ExcelUtil<BbsPost>(BbsPost.class);
        util.exportExcel(response, list, "帖子数据");
    }

    /**
     * 获取帖子详细信息
     */
    @RequiresPermissions("bbs:post:query")
    @GetMapping(value = "/{pid}")
    public AjaxResult getInfo(@PathVariable("pid") Long pid)
    {
        return success(bbsPostService.selectBbsPostByPid(pid));
    }

    /**
     * 新增帖子
     */
    @RequiresPermissions("bbs:post:add")
    @Log(title = "帖子", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BbsPost bbsPost)
    {
        return toAjax(bbsPostService.insertBbsPost(bbsPost));
    }

    /**
     * 修改帖子
     */
    @RequiresPermissions("bbs:post:edit")
    @Log(title = "帖子", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BbsPost bbsPost)
    {
        return toAjax(bbsPostService.updateBbsPost(bbsPost));
    }

    /**
     * 删除帖子
     */
    @RequiresPermissions("bbs:post:remove")
    @Log(title = "帖子", businessType = BusinessType.DELETE)
	@DeleteMapping("/{pids}")
    public AjaxResult remove(@PathVariable Long[] pids)
    {
        return toAjax(bbsPostService.deleteBbsPostByPids(pids));
    }
}
